﻿package lobby
{
	import com.sickworks.components.DateChooserComp;
	import fl.controls.TextInput;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Transform;
	import flash.text.TextField;
	import lobby.exchangeddata.BetStatusInfo;
	import lobby.events.LobbyEvent;
	import lobby.exchangeddata.HashTable;
	import lobby.common.Utility;
	import lobby.constants.ConstData;
	import lobby.exchangeddata.TransactionItemInfo;
	import lobby.exchangeddata.TransactionReportInfo;
	
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)
	 */
	public class TransactionReportPanel extends MovieClip
	{
		public var dateChoose:DateChooserComp;
		public var dateFromIcon:MovieClip;
		public var dateToIcon:MovieClip;
		public var txtDateFromTi:TextField;
		public var txtDateToTi:TextField;
		public var btnSearchReport:SimpleButton;
		
		public var txtPastTf:TextField;
		public var txtOkTf:TextField
		public var txtDateFrom:TextField;
		public var txtDateTo:TextField;
		public var txtSearch:TextField;
		
		public var txtTransactionNo:TextField;
		public var txtTransactionId:TextField;
		public var txtTransactionType:TextField;
		public var txtTransactionResultDate:TextField;
		public var txtTransactionTable:TextField;
		public var txtTransactionGame:TextField;
		public var txtTransactionResult:TextField;
		public var txtTransactionBetCode:TextField;
		public var txtTransactionAmount:TextField;
		public var txtTransactionBalance:TextField;
		public var txtTransactionComm:TextField;
		
		public var txtReportMessageTf:TextField;
		public var btnCloseReport:SimpleButton;
		
		public var txtTransactionTotalAmountTf:TextField;
		
		public var transactionMc:MovieClip;
		public var hisBoard:Sprite = new Sprite();
		public var pageControl:PageControl;
		public var transactionDetail:TransactionDetailPanel;
		
		private var isAddPageControl:Boolean = true;
		
		private var currentIconDate:MovieClip;
		private var dateFrom:Array;
		private var dateTo:Array;
		
		public function TransactionReportPanel()
		{
			hide();
			dateFrom = new Array();
			dateTo = new Array();
			this.dateChoose.visible = false;
			
			this.txtOkTf.mouseEnabled = false;
			this.txtSearch.mouseEnabled = false;
			
			this.dateFromIcon.addEventListener(MouseEvent.CLICK, dateIconClickHdl);
			this.dateToIcon.addEventListener(MouseEvent.CLICK, dateIconClickHdl);
			this.dateChoose.addEventListener(DateChooserComp.SELECTION_CHANGED, dateChooseClickHdl);
			this.btnSearchReport.addEventListener(MouseEvent.CLICK, searchReportHdl);
			
			this.btnCloseReport.addEventListener(MouseEvent.CLICK, closeHdl);
			
			transactionMc.addChild(hisBoard);
		}
		
		private function closeHdl(e:Event):void
		{
			hide();
		}
		
		private function searchReportHdl(e:Event):void
		{
			if (txtDateFromTi.text == "" || txtDateToTi.text == "")
			{
				dispatchEvent(new LobbyEvent(LobbyEvent.SHOW_ERR_DATE_NULL, true));
				return;
			}
			var dateF:Date = new Date(dateFrom[0], dateFrom[1], dateFrom[2]);
			var dateT:Date = new Date(dateTo[0], dateTo[1], dateTo[2]);
			if (dateF < dateT)
			{
				this.isAddPageControl = true;
				var event = new LobbyEvent(LobbyEvent.GET_TRANSACTION_REPORT, true);
				event.ms = 1;
				dispatchEvent(event);
			}
			else
			{
				dispatchEvent(new LobbyEvent(LobbyEvent.SHOW_ERR_CHOOSE_DATE, true));
			}
		}
		
		private function dateChooseClickHdl(e:Event):void
		{
			trace("choose");
			this.dateChoose.visible = false;
			var dateSelect:String = e.target.selectedDates[0];
			var arrayDate:Array = dateSelect.split(" ");
			dateSelect = arrayDate[2] + "/" + arrayDate[1] + "/" + arrayDate[5];
			if (currentIconDate == dateToIcon)
			{
				txtDateToTi.text = dateSelect;
				dateTo[0] = arrayDate[5];
				dateTo[1] = Utility.getMonthNumber(arrayDate[1]);
				dateTo[2] = arrayDate[2];
				dispatchEvent(new LobbyEvent(LobbyEvent.UPDATE_DATE_TO_TRANSACTION, true));
			}
			else if (currentIconDate == dateFromIcon)
			{
				txtDateFromTi.text = dateSelect;
				dateFrom[0] = arrayDate[5];
				dateFrom[1] = Utility.getMonthNumber(arrayDate[1]);
				dateFrom[2] = arrayDate[2];
				dispatchEvent(new LobbyEvent(LobbyEvent.UPDATE_DATE_FROM_TRANSACTION, true));
			}
		}
		
		private function dateIconClickHdl(e:MouseEvent):void
		{
			if (e.currentTarget == dateToIcon)
			{
				this.dateChoose.x = this.dateToIcon.x - 20;
				this.dateChoose.visible = true;
				setChildIndex(this.dateChoose, this.numChildren - 1);
				currentIconDate = dateToIcon;
			}
			else if (e.currentTarget == dateFromIcon)
			{
				this.dateChoose.x = this.dateFromIcon.x - 20;
				this.dateChoose.visible = true;
				setChildIndex(this.dateChoose, this.numChildren - 1);
				currentIconDate = dateFromIcon;
			}
		}
		
		public function updateDate(dFrom:String, dTo:String):void
		{
			this.txtDateFromTi.text = dFrom;
			this.txtDateToTi.text = dTo;
			var tempFrom:Array = dFrom.split("/");
			this.dateFrom[0] = tempFrom[2];
			this.dateFrom[1] = Utility.getMonthNumber(tempFrom[1]);
			this.dateFrom[2] = tempFrom[0];
			
			var tempTo:Array = dTo.split("/");
			this.dateTo[0] = tempTo[2];
			this.dateTo[1] = Utility.getMonthNumber(tempTo[1]);
			this.dateTo[2] = tempTo[0];
		}
		
		public function initText(hashTableInfo:HashTable):void
		{
			this.txtPastTf.text = hashTableInfo.lookupString("titleTransRep")
			this.txtOkTf.text = hashTableInfo.lookupString("btnclose");
			this.txtDateFrom.text = hashTableInfo.lookupString("titleFromDate");
			this.txtDateTo.text = hashTableInfo.lookupString("titleToDate");
			this.txtSearch.text = hashTableInfo.lookupString("btnSubmit");
			this.txtReportMessageTf.text = "";
			this.txtTransactionNo.text = hashTableInfo.lookupString("titleTransRepNo");
			this.txtTransactionId.text = hashTableInfo.lookupString("titleTransRepId");
			this.txtTransactionType.text = hashTableInfo.lookupString("titleTransRepType");
			this.txtTransactionResultDate.text = hashTableInfo.lookupString("titleTransRepDate");
			this.txtTransactionTable.text = hashTableInfo.lookupString("titleTransRepTableNo");
			this.txtTransactionGame.text = hashTableInfo.lookupString("titleTransRepGameNo");
			this.txtTransactionResult.text = hashTableInfo.lookupString("titleTransRepResult");
			this.txtTransactionBetCode.text = hashTableInfo.lookupString("titleTransRepBetCode");
			this.txtTransactionAmount.text = hashTableInfo.lookupString("titleTransRepAmount");
			this.txtTransactionBalance.text = hashTableInfo.lookupString("titleTransRepBalance");
			this.txtTransactionComm.text = hashTableInfo.lookupString("CommissionAmount");
			this.transactionDetail.initText(hashTableInfo);
		}
		
		public function updateValueTransactionReport(info:Object):void
		{
			trace("updateValueTransactionReport");
			var transactionReport:TransactionReportInfo = TransactionReportInfo(info);
			removeHistory();
			for (var i:int = 0; i < transactionReport.dataTransation.length; i++)
			{
				
				var itemData:TransactionItemInfo = transactionReport.dataTransation[i];
				var itemMc:TransactionItem = new TransactionItem(itemData, itemData.getMulti);
				itemMc.x = 0;
				itemMc.y = i * 25 + i;
				hisBoard.addChild(itemMc);
			}
			if (this.isAddPageControl == true)
			{
				if (transactionReport.pageNum > 1)
				{
					this.pageControl.addPageToPageList(this.pageControl.pageList, transactionReport.pageNum, ConstData.PAGE_CONTROL_TRANSACTION_REPORT);
					this.pageControl.visible = true;
				}
			}
			this.isAddPageControl = false;
		}
		
		public function show():void
		{
			this.visible = true;
		}
		
		public function hide():void
		{
			this.visible = false;
			this.pageControl.visible = false;
		}
		
		private function removeHistory():void
		{
			
			var numberChid:int = hisBoard.numChildren;
			if (numberChid > 0)
			{
				for (var j:int = 0; j < numberChid; j++)
				{
					var his:DisplayObject = hisBoard.removeChildAt(0);
					his = null;
				}
			}
		}
		
		public function updateValueDetailTransactionReport(info:Object):void
		{
			trace("updateValueDetailTransactionReport");
			this.transactionDetail.updateValueTransactionReport(info);
		}
	}

}